package com.example.komunikator;

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import android.widget.Toast;
import android.content.Intent;
import android.graphics.Color;

import com.example.komunikator.R.id;
import com.example.komunikator.R.layout;

import java.util.ArrayList;

public class LoginActivity extends Activity {

	private ArrayList<String> messages = new ArrayList();
	private Handler mHandler = new Handler();
	public SettingsDialog mDialog;
	public JabbLogIn mDialog2;
	private EditText mRecipient;
	private EditText mSendText;
	private ListView mList;
	public XMPPConnection connection;
	private String someString ="TestowyString";

	/**
	 * Called with the activity is first created.
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);


		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main);
		//SQLConnector sqlconnector = new SQLConnector();
		mRecipient = (EditText) this.findViewById(R.id.recipient);
		mSendText = (EditText) this.findViewById(R.id.sendText);
		mList = (ListView) this.findViewById(R.id.listMessages);
		setListAdapter();

		// Dialog for getting the xmpp settings
		mDialog = new SettingsDialog(this);
		mDialog2 = new JabbLogIn(this);

		// Set 	a listener to show the settings dialog
		//		Button setup = (Button) this.findViewById(R.id.setup);
		//		setup.setOnClickListener(new View.OnClickListener() {
		//			public void onClick(View view) {
		//				mHandler.post(new Runnable() {
		//					public void run() {
		//						mDialog.show();
		//					}
		//				});
		//			}
		//		});

		// Set a listener to send a chat text message
		Button send = (Button) this.findViewById(R.id.send);
		send.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (connection==null){
					Toast.makeText(LoginActivity.this, "Please, go to setup first, or check Your network Connection!", Toast.LENGTH_LONG).show();
				}else{

					String to = mRecipient.getText().toString();
					String text = mSendText.getText().toString();

					Log.i("XMPPClient", "Sending text [" + text + "] to [" + to
							+ "]");
					Message msg = new Message(to, Message.Type.chat);
					msg.setBody(text);
					connection.sendPacket(msg);
					messages.add(connection.getUser() + ":");
					messages.add(text);
					//
					mSendText.setText("");

					setListAdapter();
				}

			}
		});
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{


		switch (item.getItemId())
		{
		case R.id.menu_users:
			Intent intent = new Intent(LoginActivity.this, UserList.class);
			intent.putExtra("xmppConn", connection.toString());
			startActivity(intent);        	
			return true;
		case R.id.menu_preferences:


			mHandler.post(new Runnable() {
				public void run() {

					
					
					
					mDialog.show();
					mDialog2.show();
				}
			});

//			mHandler.post(new Runnable() {
//				public void run() {
//					mDialog2.show();
//				}
//			});
			
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}   

	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu_popup, menu);
		return true;
	}
	/**
	 * Called by Settings dialog when a connection is establised with the XMPP
	 * server
	 * 
	 * @param connection
	 */
	public void setConnection(XMPPConnection connection) {
		this.connection = connection;
		if (connection != null) {
			// Add a packet listener to get messages sent to us
			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener() {
				public void processPacket(Packet packet) {

					Message message = (Message) packet;
					if (message.getBody() != null) {
						String fromName = StringUtils.parseBareAddress(message
								.getFrom());
						Log.i("XMPPClient", "Got text [" + message.getBody()
								+ "] from [" + fromName + "]");
						messages.add(fromName + ":");
						messages.add(message.getBody());
						// Add the incoming message to the list view
						mHandler.post(new Runnable() {
							public void run() {
								setListAdapter();
							}
						});
					}
				}
			}, filter);
		}
	}

	private void setListAdapter() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.single_multi_line_list_item, R.id.text1, messages);
		// messages);
		mList.setAdapter(adapter);


	}
}
